package com.wangqingbo.myspringboot212.config;

import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 处理国家化语言，创建国家或地区的语言
 */
public class MassagesLocalResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        // 创建一个默认的地区
        Locale local = Locale.getDefault();
        String language = request.getParameter("language");
        if (language != null && !"".equals(language))
        {
            // 使用下划线分割参数，zh_CN
            String[] languageArray = language.split("_");
            // 根据传过来的参数，创建一个地区
            local = new Locale(languageArray[0],languageArray[1]);
        }
        return local;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
